import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import java.util.*;
import javax.swing.*;

import com.jgoodies.forms.factories.*;
import com.jgoodies.forms.layout.*;

/**
 * @author Pribytkoyski Alexey
 */
public class Frame extends JFrame {
	TestManager tm = null;

	public Frame(TestManager testManager) {
		tm = testManager;
		initComponents();
		radioButtonMenuItem1MousePressed();
		textPane2.setText(tm.getHelp());
		comboBox1.setModel(new DefaultComboBoxModel(tm.loadTestsList()));
		button1.setEnabled(false);
	}

	private void button1MousePressed() {
		String textIn = textField1.getText();
		if(textIn.trim().equals("")) {
			return;
		}
		if (tm.getMode() == 2 || tm.getMode() == 3) {
			tm.setAnswer(textIn);
			textPane1.setText(tm.getQuestion());
		}
		if (tm.getMode() == 4) {
			button1.setText("ещё");
			tm.setMode(5);
			return;
		}
		
		if (tm.getMode() == 5){
			button1.setEnabled(false);
			comboBox1.setEnabled(true);
			comboBox1.setModel(new DefaultComboBoxModel(tm.loadTestsList()));
			tm.setMode(2);
		}
	}

	private void radioButtonMenuItem1MousePressed() {
		Locale.setDefault(new Locale("be_BY"));
		radioButtonMenuItem1.doClick();
		initComponentsI18n();
	}

	private void radioButtonMenuItem2MousePressed() {
		Locale.setDefault(new Locale("ru_RU"));
		radioButtonMenuItem2.doClick();
		initComponentsI18n();
	}

	private void radioButtonMenuItem3MousePressed() {
		Locale.setDefault(new Locale("en_US"));
		radioButtonMenuItem3.doClick();
		initComponentsI18n();
	}

	private void menuItem2MousePressed() {
		popupMenu1.pack();
		popupMenu1.setLocation(textPane1.getLocationOnScreen());
		popupMenu1.setVisible(true);
	}

	private void textPane2MousePressed() {
		popupMenu1.setVisible(false);
	}

	private void comboBox1ItemStateChanged() {
		tm.selectTest(String.valueOf(comboBox1.getSelectedIndex()));
		tm.setMode(2);
		textPane1.setText(tm.getQuestion());
		comboBox1.setEnabled(false);
		button1.setEnabled(true);
		button1.setText(ResourceBundle.getBundle("media.locale").getString("f.b1.text"));
	}


	private void initComponents() {
		//VV _ JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
		menuBar1 = new JMenuBar();
		menu1 = new JMenu();
		menuItem1 = new JMenuItem();
		menu3 = new JMenu();
		radioButtonMenuItem1 = new JRadioButtonMenuItem();
		radioButtonMenuItem2 = new JRadioButtonMenuItem();
		radioButtonMenuItem3 = new JRadioButtonMenuItem();
		menu2 = new JMenu();
		menuItem2 = new JMenuItem();
		panel1 = new JPanel();
		label1 = new JLabel();
		comboBox1 = new JComboBox();
		scrollPane1 = new JScrollPane();
		textPane1 = new JTextPane();
		panel2 = new JPanel();
		label3 = new JLabel();
		textField1 = new JTextField();
		button1 = new JButton();
		label2 = new JLabel();
		popupMenu1 = new JPopupMenu();
		scrollPane2 = new JScrollPane();
		textPane2 = new JTextPane();

		//======== this ========
		Container contentPane = getContentPane();
		contentPane.setLayout(new FormLayout(
			"[5dlu,default], $lcgap, default:grow, $lcgap, [5dlu,default]",
			"[5dlu,default], $lgap, default:grow, $lgap, default, $lgap, [default,10dlu]"));

		//======== menuBar1 ========
		{

			//======== menu1 ========
			{
				menu1.add(menuItem1);
			}
			menuBar1.add(menu1);

			//======== menu3 ========
			{

				//---- radioButtonMenuItem1 ----
				radioButtonMenuItem1.addMouseListener(new MouseAdapter() {
					@Override
					public void mousePressed(MouseEvent e) {
						radioButtonMenuItem1MousePressed();
					}
				});
				menu3.add(radioButtonMenuItem1);

				//---- radioButtonMenuItem2 ----
				radioButtonMenuItem2.addMouseListener(new MouseAdapter() {
					@Override
					public void mousePressed(MouseEvent e) {
						radioButtonMenuItem2MousePressed();
					}
				});
				menu3.add(radioButtonMenuItem2);

				//---- radioButtonMenuItem3 ----
				radioButtonMenuItem3.addMouseListener(new MouseAdapter() {
					@Override
					public void mousePressed(MouseEvent e) {
						radioButtonMenuItem3MousePressed();
					}
				});
				menu3.add(radioButtonMenuItem3);
			}
			menuBar1.add(menu3);

			//======== menu2 ========
			{

				//---- menuItem2 ----
				menuItem2.addMouseListener(new MouseAdapter() {
					@Override
					public void mousePressed(MouseEvent e) {
						menuItem2MousePressed();
					}
				});
				menu2.add(menuItem2);
			}
			menuBar1.add(menu2);
		}
		setJMenuBar(menuBar1);

		//======== panel1 ========
		{
			panel1.setLayout(new FormLayout(
				"default, $lcgap, [5dlu,default], $lcgap, default:grow",
				"default, $lgap, [3dlu,default], $lgap, default:grow"));
			panel1.add(label1, CC.xy(1, 1));

			//---- comboBox1 ----
			comboBox1.setSelectedIndex(-1);
			comboBox1.addItemListener(new ItemListener() {
				@Override
				public void itemStateChanged(ItemEvent e) {
					if(e.getStateChange() == ItemEvent.SELECTED) {
						comboBox1ItemStateChanged();
					}
				}
			});
			panel1.add(comboBox1, CC.xy(5, 1));

			//======== scrollPane1 ========
			{

				//---- textPane1 ----
				textPane1.setEditable(false);
				scrollPane1.setViewportView(textPane1);
			}
			panel1.add(scrollPane1, CC.xywh(1, 4, 5, 2));
		}
		contentPane.add(panel1, CC.xywh(3, 2, 1, 2));

		//======== panel2 ========
		{
			panel2.setLayout(new FormLayout(
				"default, $lcgap, default:grow, $lcgap, default",
				"default"));
			panel2.add(label3, CC.xy(1, 1));
			panel2.add(textField1, CC.xy(3, 1));

			//---- button1 ----
			button1.addMouseListener(new MouseAdapter() {
				@Override
				public void mousePressed(MouseEvent e) {
					button1MousePressed();
				}
			});
			panel2.add(button1, CC.xy(5, 1));
		}
		contentPane.add(panel2, CC.xy(3, 5));
		contentPane.add(label2, CC.xywh(2, 7, 4, 1));
		pack();
		setLocationRelativeTo(getOwner());

		//======== popupMenu1 ========
		{

			//======== scrollPane2 ========
			{

				//---- textPane2 ----
				textPane2.setText("SPARTA!");
				textPane2.setEditable(false);
				textPane2.addMouseListener(new MouseAdapter() {
					@Override
					public void mousePressed(MouseEvent e) {
						textPane2MousePressed();
					}
				});
				scrollPane2.setViewportView(textPane2);
			}
			popupMenu1.add(scrollPane2);
		}

		//---- buttonGroup1 ----
		ButtonGroup buttonGroup1 = new ButtonGroup();
		buttonGroup1.add(radioButtonMenuItem1);
		buttonGroup1.add(radioButtonMenuItem2);
		buttonGroup1.add(radioButtonMenuItem3);

		initComponentsI18n();
		//VV _ JFormDesigner - End of component initialization  //GEN-END:initComponents
	}

	private void initComponentsI18n() {
		//VV _ JFormDesigner - Component i18n initialization - DO NOT MODIFY  //GEN-BEGIN:initI18n
		ResourceBundle bundle = ResourceBundle.getBundle("media.locale");
		setTitle(bundle.getString("f.title"));
		menu1.setText(bundle.getString("f.m1.text"));
		menuItem1.setText(bundle.getString("f.mIt1.text"));
		menu3.setText(bundle.getString("f.m3.text"));
		radioButtonMenuItem1.setText(bundle.getString("f.rBMIt.text"));
		radioButtonMenuItem2.setText(bundle.getString("f.rBMIt2.text"));
		radioButtonMenuItem3.setText(bundle.getString("f.rBMIt3.text"));
		menu2.setText(bundle.getString("f.m2.text"));
		menuItem2.setText(bundle.getString("f.mIt2.text"));
		label1.setText(bundle.getString("f.l1.text"));
		label3.setText(bundle.getString("f.l3.text"));
		button1.setText(bundle.getString("f.b1.text"));
		label2.setText(bundle.getString("f.l2.text"));
		//VV _ JFormDesigner - End of component i18n initialization  //GEN-END:initI18n
	}

	// VV _ JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
	private JMenuBar menuBar1;
	private JMenu menu1;
	private JMenuItem menuItem1;
	private JMenu menu3;
	private JRadioButtonMenuItem radioButtonMenuItem1;
	private JRadioButtonMenuItem radioButtonMenuItem2;
	private JRadioButtonMenuItem radioButtonMenuItem3;
	private JMenu menu2;
	private JMenuItem menuItem2;
	private JPanel panel1;
	private JLabel label1;
	private JComboBox comboBox1;
	private JScrollPane scrollPane1;
	private JTextPane textPane1;
	private JPanel panel2;
	private JLabel label3;
	private JTextField textField1;
	private JButton button1;
	private JLabel label2;
	private JPopupMenu popupMenu1;
	private JScrollPane scrollPane2;
	private JTextPane textPane2;
	// VV _ JFormDesigner - End of variables declaration  //GEN-END:variables
}
